<?php
/**
 * home.php
 * @author ranuandra
 *
 * 8 Agu 2011 11:47:15
 *
 * 
 */

class Controller_Home extends Kohana_Polreg
{
	public $template = 'home';
	public function action_index()
	{
		
		
		//$query = DB::query(Database::SELECT, 'SELECT * FROM roles');
//		$result = ORM::factory('role')->find_all();
//		
//		foreach($result as $user)
//		{
//		    // Send reminder email to $user['email']
//		    echo $user->rolename;
//		}
		/*$menu = View::factory('module/menu');
		$this->template->menu = $menu;
		$this->template->container = "Selamat Datang";*/
		$this->auto_render = false;
/*       try 
		{
			$_POST['username'] = "test";
			$_POST['password'] = "12345678";
			$_POST['password_confirm'] = "12345678";
			$_POST['email'] = "adi@email.com";
			
			
			
			$user = ORM::factory('user');
			
			$user->create_user($_POST, array(
		 										'username',
												'password',
		 										'email',
		  									));
	  		$login_role = new Model_Role(array('name' =>'login'));
			$user->add('roles',$login_role);
	  		$login_role = new Model_Role(array('name' =>'admin'));
			$user->add('roles',$login_role);
			
		}
		catch (ORM_Validation_Exception $e) {
			$errors = $e->errors('models');
			print_r($errors);
		}
		
		exit;*/
		
		
		if (Auth_ORM::instance()->logged_in() )
		{

			Request::current()->redirect(URL::base().'report');
			exit;

		}
		else 
		{
			$view = View::factory('login/form');
			$this->template->container = $view;	

		}		
		
	}
	
	public function action_login()
	{
		$this->template = "home";
		if(isset($_POST['username']) and isset($_POST['password']))
		{
			$user = Auth_ORM::instance();
			
			//echo $_POST['password'];
			//$status = Auth_ORM::instance()->login($_POST['username'], $_POST['password'], FALSE);
			//echo "dsadasd";
			//print_r($status);
			$status = Auth_ORM::instance()->login($_POST['username'], $_POST['password'], FALSE);

			if($status==1)
			{
				Request::current()->redirect(URL::base()."report");
			}
			else 
			{
				Request::current()->redirect(URL::base());
			}
			
			//print_r($status);
			//exit;
			//exit;
				
		}		
	}
	
	public function Action_Logout()
	{
		Auth_ORM::instance()->logout();
		echo "<script>window.location.replace(\"".URL::base()."\")</script>";
		exit;
		//Request::current()->redirect(URL::base());
	}
	
}

?>
